package com.eptok.gw.szcups.cups.message;

import ys8583.parse.IOMessage;

import com.eptok.gw.szcups.common.GateWayConstant;
import com.eptok.gw.szcups.common.KeyContainer;
import com.eptok.gw.szcups.common.PropertyLoader;
import com.eptok.gw.szcups.hsm.HsmService;
import com.eptok.gw.szcups.pojo.BkCardBncQry;
import com.eptok.gw.szcups.pojo.CancelCardPay;
import com.eptok.gw.szcups.pojo.CardPay;
import com.eptok.gw.szcups.pojo.CtCardRepayment;
import com.eptok.gw.szcups.pojo.Relationship;
import com.eptok.gw.szcups.pojo.SigleColt;
import com.eptok.gw.szcups.util.DateUtil;
import com.eptok.gw.szcups.util.SerialNumber;
import com.solab.iso8583.common.ByteUtil;

/**
 *@author:heqishan, Email:heqishan@gmail.com
 *2013-3-5 下午07:46:43
 */
public class PackMessageUtil {
	
	//冲正交易不需要填写的域
	private final static int[] deleteField = {22, 26, 35, 36, 52, 53, 60};
	
	/**
	 * 心跳包
	 * @return
	 */
	public static byte[] packHeartbeatMessage(){
		return new byte[0];
	}
	
	/**
	 * 签到请求报文
	 * @return
	 * @throws Exception 
	 */
	public static IOMessage packSignReq() throws Exception{
		IOMessage ioMessage = new IOMessage(GateWayConstant.MSG_CODE_TEMPLATE_ID);
		ioMessage.setRspMesType(GateWayConstant.MSG_CODE_SING_REQ);
		ioMessage.setRspField(11, SerialNumber.getChanSequence());			//系统跟踪号
		ioMessage.setRspField(12, DateUtil.formatDate("HHmmss"));						//本地交易时间	hhmmss
		ioMessage.setRspField(13, DateUtil.formatDate("MMdd"));							//本地交易日期	MMDD
		ioMessage.setRspField(33, PropertyLoader.getInstance().getChanForwardPosID());		//发送机构标识码
		ioMessage.setRspField(41, PropertyLoader.getInstance().getChanTerminalID());		//TODO 是否需要左补0 受卡机终端标识码
		ioMessage.setRspField(70, GateWayConstant.NETWORK_MESSAGE_CODE);				//网络管理信息码
		ioMessage.createResponseMsg();
		return ioMessage;
	}
	
	/**
	 * 建立/撤销委托关系
	 * @param relationship
	 * @return
	 * @throws Exception
	 */
	public static IOMessage packRelationship(Relationship relationship) throws Exception{
		IOMessage ioMessage = new IOMessage(GateWayConstant.MSG_CODE_TEMPLATE_ID);
		ioMessage.setRspMesType(GateWayConstant.MSG_CODE_RELATIONSHIP_REQ);
		ioMessage.setRspField(2, relationship.getPosData().getCardNum());							//主账号
		String handleCode;																//处理码
		if(relationship.getReqType().equals("01")){
			handleCode = GateWayConstant.FIELD_3_RELATIONSHIP_BUILD;
		}else{
			handleCode = GateWayConstant.FIELD_3_RELATIONSHIP_CANCEL;
		}
		ioMessage.setRspField(3, handleCode);
		ioMessage.setRspField(7, DateUtil.formatDate("MMddHHmmss"));
		ioMessage.setRspField(11, SerialNumber.getChanSequence());		//系统跟踪号
		ioMessage.setRspField(12, DateUtil.formatDate("HHmmss"));						//本地交易时间	hhmmss
		ioMessage.setRspField(13, DateUtil.formatDate("MMdd"));							//本地交易日期	MMDD
		ioMessage.setRspField(22, relationship.getPosData().getEntryMode());
		if(GateWayConstant.FIELD_22.equals(relationship.getPosData().getEntryMode())){
			ioMessage.setRspField(26, GateWayConstant.FIELD_26);						//服务点PIN 采集代码
			//转加密pin
			String newPin = new HsmService().pin_tpk2zpk(relationship.getPosData().getTpk(), 
					KeyContainer.getInstance().getChanZPK(), relationship.getPosData().getPin(), relationship.getPosData().getCardNum());
			ioMessage.setRspField(52, ByteUtil.hexStringToBytes(newPin));				//个人密码
			ioMessage.setRspField(53, GateWayConstant.FIELD_53);						//安全控制信息
		}
		
		
		ioMessage.setRspField(25, GateWayConstant.FIELD_25_RELATIONSHIP);				//服务点条件码
		ioMessage.setRspField(32, PropertyLoader.getInstance().getChanAcquirerPosID());		//受理机构标识码
		ioMessage.setRspField(33, PropertyLoader.getInstance().getChanForwardPosID());		//发送机构标识码(填接入渠道代码)
		
		String track2 = relationship.getPosData().getTrack2();											//磁道2
		if(track2.length() > 37){
			track2 = track2.substring(0,37);
		}
		ioMessage.setRspField(35, track2);													
		
		if(relationship.getPosData().getTrack3()!= null && relationship.getPosData().getTrack3().length() > 0){		//磁道3
			String track3 = relationship.getPosData().getTrack3();
			if(track3.length() > 104){
				track3 = track3.substring(0,104);
			}
			ioMessage.setRspField(36, track3);
		}
		
		ioMessage.setRspField(37, SerialNumber.getChanRetrievalNumber());				//系统检索号
		ioMessage.setRspField(41, PropertyLoader.getInstance().getChanTerminalID());		//受卡机终端标识码
		ioMessage.setRspField(42, PropertyLoader.getInstance().getChanParterID());			//受卡方标识码
		ioMessage.setRspField(43, ByteUtil.fillString(PropertyLoader.getInstance().getChanParterName(), ' ', 40, true));	//受卡方名称地址
		
		StringBuffer fild48 = new StringBuffer();
		fild48.append(GateWayConstant.FIELD_48_RELATIONSHIP_TAG);
		fild48.append(GateWayConstant.FIELD_48_RELATIONSHIP_USERTYPE);
		String payNo = ByteUtil.fillString(relationship.getPayNo(), ' ', 40, true);
		fild48.append(payNo);
		fild48.append(GateWayConstant.FIELD_48_RELATIONSHIP_AREACODE1);
		fild48.append(GateWayConstant.FIELD_48_RELATIONSHIP_AREACODE2);
		fild48.append(GateWayConstant.FIELD_48_RELATIONSHIP_MARK);
		fild48.append(GateWayConstant.FIELD_48_RELATIONSHIP_USERTYPE);
		fild48.append(payNo);
		fild48.append(GateWayConstant.FIELD_48_RELATIONSHIP_TIME_LIMIT);
		fild48.append(ByteUtil.fillString("", ' ', 30, true));
		fild48.append(GateWayConstant.FIELD_48_END);
		ioMessage.setRspField(48, fild48.toString());											//自定义域
		
		ioMessage.setRspField(49, GateWayConstant.FIELD_49);						//交易货币代码
		ioMessage.setRspField(60, GateWayConstant.FIELD_60);						//自定义域
		//ioMessage.setRspField(61, "01" + relationship.getPaperwork());				//持卡人身份认证信息
		//生成mac
		new MacGenerator().generateChanMac(ioMessage);
		ioMessage.createResponseMsg();
		return ioMessage;
	}
	
	/**
	 * 消费
	 * @param cardPay
	 * @return
	 * @throws Exception
	 */
	public static IOMessage packCardPay(CardPay cardPay) throws Exception{
		IOMessage ioMessage = new IOMessage(GateWayConstant.MSG_CODE_TEMPLATE_ID);
		ioMessage.setRspMesType(GateWayConstant.MSG_CODE_CARDPAY_REQ);
		ioMessage.setRspField(2, cardPay.getPosData().getCardNum());											//主账号
		ioMessage.setRspField(3, GateWayConstant.FIELD_3_CARDPAY);								//处理码
		ioMessage.setRspField(4, ByteUtil.fillString(cardPay.getAmount(), '0', 12, false));		//交易金额
		ioMessage.setRspField(7, DateUtil.formatDate("MMddHHmmss"));
		ioMessage.setRspField(11, SerialNumber.getChanSequence());			//系统跟踪号
		ioMessage.setRspField(12, DateUtil.formatDate("HHmmss"));						//本地交易时间	hhmmss
		ioMessage.setRspField(13, DateUtil.formatDate("MMdd"));							//本地交易日期	MMDD
		
		ioMessage.setRspField(22,cardPay.getPosData().getEntryMode());
		if(GateWayConstant.FIELD_22.equals(cardPay.getPosData().getEntryMode())){
			ioMessage.setRspField(26, GateWayConstant.FIELD_26);						//服务点PIN 采集代码
			//转加密pin
			String newPin = new HsmService().pin_tpk2zpk(cardPay.getPosData().getTpk(), 
					KeyContainer.getInstance().getChanZPK(), cardPay.getPosData().getPin(), cardPay.getPosData().getCardNum());
			ioMessage.setRspField(52, ByteUtil.hexStringToBytes(newPin));				//个人密码
			ioMessage.setRspField(53, GateWayConstant.FIELD_53);						//安全控制信息
		}
		
		ioMessage.setRspField(25, GateWayConstant.FIELD_25_CARDPAY);					//服务点条件码
		ioMessage.setRspField(32, PropertyLoader.getInstance().getChanAcquirerPosID());		//受理机构标识码
		ioMessage.setRspField(33, PropertyLoader.getInstance().getChanForwardPosID());		//发送机构标识码(填接入渠道代码)
		
		String track2 = cardPay.getPosData().getTrack2();											//磁道2
		if(track2.length() > 37){
			track2 = track2.substring(0,37);
		}
		ioMessage.setRspField(35, track2);													
		
		if(cardPay.getPosData().getTrack3()!= null && cardPay.getPosData().getTrack3().length() > 0){		//磁道3
			String track3 = cardPay.getPosData().getTrack3();
			if(track3.length() > 104){
				track3 = track3.substring(0,104);
			}
			ioMessage.setRspField(36, track3);
		}
		
		ioMessage.setRspField(37, SerialNumber.getChanRetrievalNumber());				//TODO 系统检索号(重新生成)
		ioMessage.setRspField(41, PropertyLoader.getInstance().getChanTerminalID());		//受卡机终端标识码
		ioMessage.setRspField(42, PropertyLoader.getInstance().getChanParterID());			//受卡方标识码
		ioMessage.setRspField(43, ByteUtil.fillString(PropertyLoader.getInstance().getChanParterName(), ' ', 40, true));	//受卡方名称地址
		
		ioMessage.setRspField(49, GateWayConstant.FIELD_49);						//交易货币代码
		ioMessage.setRspField(60, GateWayConstant.FIELD_60);						//自定义域
		//生成mac
		new MacGenerator().generateChanMac(ioMessage);
		ioMessage.createResponseMsg();
		return ioMessage;
	}
	
	/**
	 * 消费撤销
	 * @param cancel
	 * @return
	 * @throws Exception
	 */
	public static IOMessage packCancelCardPay(CancelCardPay cancel) throws Exception{
		IOMessage ioMessage = new IOMessage(GateWayConstant.MSG_CODE_TEMPLATE_ID);
		ioMessage.setRspMesType(GateWayConstant.MSG_CODE_CARDPAY_REQ);
		ioMessage.setRspField(2, cancel.getPosData().getCardNum());											//主账号
		ioMessage.setRspField(3, GateWayConstant.FIELD_3_CANCEL_CARDPAY);						//处理码
		ioMessage.setRspField(4, ByteUtil.fillString(cancel.getAmount(), '0', 12, false));		//交易金额
		ioMessage.setRspField(7, DateUtil.formatDate("MMddHHmmss"));
		String chanTrack = SerialNumber.getChanSequence();
		ioMessage.setRspField(11, chanTrack);			//系统跟踪号
		ioMessage.setRspField(12, DateUtil.formatDate("HHmmss"));						//本地交易时间	hhmmss
		ioMessage.setRspField(13, DateUtil.formatDate("MMdd"));							//本地交易日期	MMDD
		ioMessage.setRspField(22,cancel.getPosData().getEntryMode());
		
		ioMessage.setRspField(25, GateWayConstant.FIELD_25_CARDPAY);						//服务点条件码
		if(GateWayConstant.FIELD_22.equals(cancel.getPosData().getEntryMode())){
			ioMessage.setRspField(26, GateWayConstant.FIELD_26);						//服务点PIN 采集代码
			//转加密pin
			String newPin = new HsmService().pin_tpk2zpk(cancel.getPosData().getTpk(), 
					KeyContainer.getInstance().getChanZPK(), cancel.getPosData().getPin(), cancel.getPosData().getCardNum());
			ioMessage.setRspField(52, ByteUtil.hexStringToBytes(newPin));				//个人密码
			ioMessage.setRspField(53, GateWayConstant.FIELD_53);						//安全控制信息
		}
		ioMessage.setRspField(32, PropertyLoader.getInstance().getChanAcquirerPosID());		//受理机构标识码
		ioMessage.setRspField(33, PropertyLoader.getInstance().getChanForwardPosID());		//发送机构标识码(填接入渠道代码)
		
		String track2 = cancel.getPosData().getTrack2();											//磁道2
		if(track2.length() > 37){
			track2 = track2.substring(0,37);
		}
		ioMessage.setRspField(35, track2);													
		
		if(cancel.getPosData().getTrack3()!= null && cancel.getPosData().getTrack3().length() > 0){			//磁道3
			String track3 = cancel.getPosData().getTrack3();
			if(track3.length() > 104){
				track3 = track3.substring(0,104);
			}
			ioMessage.setRspField(36, track3);
		}
		
		ioMessage.setRspField(37, cancel.getCupsResult().getRetrievalNumber());				//原系统检索号
		ioMessage.setRspField(41, cancel.getCupsResult().getAcceptTermianl());
		ioMessage.setRspField(42, cancel.getCupsResult().getAcceptCode());
		ioMessage.setRspField(43, ByteUtil.fillString(PropertyLoader.getInstance().getChanParterName(), ' ', 40, true));
		
		ioMessage.setRspField(49, GateWayConstant.FIELD_49);
		ioMessage.setRspField(60, GateWayConstant.FIELD_60);						//自定义域
		  
		//拼装90#
		StringBuffer field90 = new StringBuffer();
		field90.append(GateWayConstant.MSG_CODE_CARDPAY_REQ);
		field90.append(cancel.getCupsResult().getSytemTrace());
		field90.append(cancel.getCupsResult().getTransDate());
		field90.append(ByteUtil.fillString(PropertyLoader.getInstance().getChanAcquirerPosID(), '0', 11, false));
		field90.append(ByteUtil.fillString(PropertyLoader.getInstance().getChanForwardPosID(), '0', 11, false));
		ioMessage.setRspField(90, field90.toString());
		//生成mac
		new MacGenerator().generateChanMac(ioMessage);
		ioMessage.createResponseMsg();
		return ioMessage;
	}
	
	/**
	 * 消费退货
	 * @param cancel
	 * @return
	 * @throws Exception
	 */
	public static IOMessage packReturnCardPay(CancelCardPay cancel) throws Exception{
		IOMessage ioMessage = new IOMessage(GateWayConstant.MSG_CODE_TEMPLATE_ID);
		ioMessage.setRspMesType(GateWayConstant.MSG_CODE_CARDPAY_RETURN_REQ);
		ioMessage.setRspField(2, cancel.getPosData().getCardNum());											//主账号
		ioMessage.setRspField(3, GateWayConstant.FIELD_3_RETURN_CARDPAY);						//处理码
		ioMessage.setRspField(4, ByteUtil.fillString(cancel.getAmount(), '0', 12, false));		//交易金额
		ioMessage.setRspField(7, DateUtil.formatDate("MMddHHmmss"));
		ioMessage.setRspField(11, SerialNumber.getChanSequence());			//系统跟踪号
		ioMessage.setRspField(12, DateUtil.formatDate("HHmmss"));						//本地交易时间	hhmmss
		ioMessage.setRspField(13, DateUtil.formatDate("MMdd"));							//本地交易日期	MMDD
		
		ioMessage.setRspField(22,cancel.getPosData().getEntryMode());
		
		ioMessage.setRspField(25, GateWayConstant.FIELD_25_CARDPAY);						//服务点条件码
		//TODO 暂时屏蔽掉
//		if(GateWayConstant.FIELD_22.equals(cancel.getPosData().getEntryMode())){
//			ioMessage.setRspField(26, GateWayConstant.FIELD_26);						//服务点PIN 采集代码
//			//转加密pin
//			String newPin = new HsmService().pin_tpk2zpk(cancel.getPosData().getTpk(), 
//					KeyContainer.getInstance().getChanZPK(), cancel.getPosData().getPin(), cancel.getPosData().getCardNum());
//			ioMessage.setRspField(52, ByteUtil.hexStringToBytes(newPin));				//个人密码
//			ioMessage.setRspField(53, GateWayConstant.FIELD_53);						//安全控制信息
//		}
		ioMessage.setRspField(32, PropertyLoader.getInstance().getChanAcquirerPosID());		//受理机构标识码
		ioMessage.setRspField(33, PropertyLoader.getInstance().getChanForwardPosID());		//发送机构标识码(填接入渠道代码)
		
		String track2 = cancel.getPosData().getTrack2();											//磁道2
		if(track2.length() > 37){
			track2 = track2.substring(0,37);
		}
		ioMessage.setRspField(35, track2);													
		
		if(cancel.getPosData().getTrack3()!= null && cancel.getPosData().getTrack3().length() > 0){			//磁道3
			String track3 = cancel.getPosData().getTrack3();
			if(track3.length() > 104){
				track3 = track3.substring(0,104);
			}
			ioMessage.setRspField(36, track3);
		}
		
		ioMessage.setRspField(37, cancel.getCupsResult().getRetrievalNumber());				//原系统检索号
		ioMessage.setRspField(41, cancel.getCupsResult().getAcceptTermianl());
		ioMessage.setRspField(42, cancel.getCupsResult().getAcceptCode());
		ioMessage.setRspField(43, ByteUtil.fillString(PropertyLoader.getInstance().getChanParterName(), ' ', 40, true));
		
		ioMessage.setRspField(49, GateWayConstant.FIELD_49);
		ioMessage.setRspField(60, GateWayConstant.FIELD_60);						//自定义域
		
		//拼装90#
		StringBuffer field90 = new StringBuffer();
		field90.append(GateWayConstant.MSG_CODE_CARDPAY_REQ);
		field90.append(cancel.getCupsResult().getSytemTrace());
		field90.append(cancel.getCupsResult().getTransDate());
		field90.append(ByteUtil.fillString(PropertyLoader.getInstance().getChanAcquirerPosID(), '0', 11, false));
		field90.append(ByteUtil.fillString(PropertyLoader.getInstance().getChanForwardPosID(), '0', 11, false));
		ioMessage.setRspField(90, field90.toString());
		//生成mac
		new MacGenerator().generateChanMac(ioMessage);
		ioMessage.createResponseMsg();
		return ioMessage;
	}
	
	/**
	 * 冲正
	 * @param reversal
	 * @return
	 */
	public static IOMessage packReversal(String oldHexData) throws Exception{
		IOMessage ioMessage = new IOMessage(GateWayConstant.MSG_CODE_TEMPLATE_ID,ByteUtil.hexStringToBytes(oldHexData));
		ioMessage.setRspMap(ioMessage.getReqMap());
		
		//拼装90#
		StringBuffer field90 = new StringBuffer();
		field90.append(ioMessage.getReqMessageID());
		field90.append(ioMessage.getRspField(11));
		field90.append(ioMessage.getRspField(7));
		field90.append(ByteUtil.fillString((String)ioMessage.getRspField(32), '0', 11, false));
		field90.append(ByteUtil.fillString((String)ioMessage.getRspField(33), '0', 11, false));
		ioMessage.setRspField(90, field90.toString());
		
		ioMessage.setRspMesType(GateWayConstant.MSG_CODE_REVERSAL_REQ);
		if(ioMessage.getRspMessageID().equals(GateWayConstant.MSG_CODE_COLLECTION_REQ) &&
				ioMessage.getRspField(3).equals(GateWayConstant.FIELD_3_SIGLECOLT))
		{
			ioMessage.setRspField(37, SerialNumber.getActiveRetrievalNumber());				//系统检索号
		}else{
			ioMessage.setRspField(37, SerialNumber.getChanRetrievalNumber());	
		}
		
		//删除不需要的域
		for(int i=0; i< deleteField.length; i++){
			ioMessage.removeField(deleteField[i]);
		}
		//生成mac
		new MacGenerator().generateChanMac(ioMessage);
		ioMessage.createResponseMsg();
		return ioMessage;
	}
	
	
//	/**
//	 * 转换为8583-37域的系统检索号
//	 * @param tranceID	请求流水号
//	 * @return
//	 */
//	public static String convertSearchNo(String tranceID){
//		String searchNo;
//		int tranceIDLen = tranceID.length();
//		if(tranceIDLen > 12){
//			searchNo = tranceID.substring(tranceIDLen-12,tranceIDLen);
//		}else{
//			searchNo = ByteUtil.fillString(tranceID, '0', 12, true);
//		}
//		return searchNo;
//	}
	
	
	/**
	 * 组装银行卡余额查询报文
	 * @return
	 * @throws Exception
	 */
	public static IOMessage packCardBlanceQry(BkCardBncQry cardPay)throws Exception{
		IOMessage ioMessage = new IOMessage(GateWayConstant.MSG_CODE_TEMPLATE_ID);
		ioMessage.setRspMesType(GateWayConstant.MSG_CODE_CARDQUERY_REQ); //报文类型	n4
		ioMessage.setRspField(2, cardPay.getPosData().getCardNum());//主账号
		ioMessage.setRspField(3, GateWayConstant.FIELD_3_BCDBLANCE_QRY); // 3 处理码	n6 	"300000"
		ioMessage.setRspField(7, DateUtil.formatDate("MMddHHmmss"));
		ioMessage.setRspField(11, SerialNumber.getChanSequence());			//系统跟踪号
		ioMessage.setRspField(12, DateUtil.formatDate("HHmmss"));						//本地交易时间	hhmmss
		ioMessage.setRspField(13, DateUtil.formatDate("MMdd"));							//本地交易日期	MMDD
		
		ioMessage.setRspField(22,cardPay.getPosData().getEntryMode());
		if(GateWayConstant.FIELD_22.equals(cardPay.getPosData().getEntryMode())){
			ioMessage.setRspField(26, GateWayConstant.FIELD_26);						//服务点PIN 采集代码
			//转加密pin
			String newPin = new HsmService().pin_tpk2zpk(cardPay.getPosData().getTpk(), 
					KeyContainer.getInstance().getChanZPK(), cardPay.getPosData().getPin(), cardPay.getPosData().getCardNum());
			ioMessage.setRspField(52, ByteUtil.hexStringToBytes(newPin));				//个人密码
			ioMessage.setRspField(53, GateWayConstant.FIELD_53);						//安全控制信息
		}
		
		ioMessage.setRspField(25, GateWayConstant.FIELD_25_CARD_QUERY); // 25 服务点条件码	n2	M	M	“00”
		ioMessage.setRspField(32, PropertyLoader.getInstance().getChanAcquirerPosID());		//受理机构标识码
		ioMessage.setRspField(33, PropertyLoader.getInstance().getChanForwardPosID());		//发送机构标识码(填接入渠道代码)
		
		String track2 = cardPay.getPosData().getTrack2();											//磁道2
		if(track2.length() > 37){
			track2 = track2.substring(0,37);
		}
		ioMessage.setRspField(35, track2);													
		
		if(cardPay.getPosData().getTrack3()!= null && cardPay.getPosData().getTrack3().length() > 0){		//磁道3
			String track3 = cardPay.getPosData().getTrack3();
			if(track3.length() > 104){
				track3 = track3.substring(0,104);
			}
			ioMessage.setRspField(36, track3);
		}
		
		ioMessage.setRspField(37, SerialNumber.getChanRetrievalNumber());				//系统检索号
		ioMessage.setRspField(41, PropertyLoader.getInstance().getChanTerminalID());		//受卡机终端标识码
		ioMessage.setRspField(42, PropertyLoader.getInstance().getChanParterID());			//受卡方标识码
		ioMessage.setRspField(43, ByteUtil.fillString(PropertyLoader.getInstance().getChanParterName(), ' ', 40, true));	//受卡方名称地址
		
		ioMessage.setRspField(49, GateWayConstant.FIELD_49);						//交易货币代码
		ioMessage.setRspField(60, GateWayConstant.FIELD_60);						//自定义域
		new MacGenerator().generateChanMac(ioMessage); 								//生成mac
		ioMessage.createResponseMsg();
		return ioMessage;
	}
	
	/**
	 * 组装信用卡还款报文
	 * @return
	 * @throws Exception
	 */
	public static IOMessage packCtCardRepayment(CtCardRepayment bean)throws Exception{
		IOMessage ioMessage = new IOMessage(GateWayConstant.MSG_CODE_TEMPLATE_ID);
		ioMessage.setRspMesType(GateWayConstant.MSG_CODE_CREDIT_CARD_REPAY_REQ); 				//报文类型	n4
		ioMessage.setRspField(2, bean.getPosData().getCardNum());//主账号
		ioMessage.setRspField(3, GateWayConstant.FIELD_3_CTDREPAYMENT); 						// 3 处理码	n6 	"190000"
		ioMessage.setRspField(4, ByteUtil.fillString(bean.getAmount(), '0', 12, false));		//交易金额
		ioMessage.setRspField(7, DateUtil.formatDate("MMddHHmmss"));
		ioMessage.setRspField(11, SerialNumber.getChanSequence());			//系统跟踪号
		ioMessage.setRspField(12, DateUtil.formatDate("HHmmss"));						//本地交易时间	hhmmss
		ioMessage.setRspField(13, DateUtil.formatDate("MMdd"));							//本地交易日期	MMDD
		
		ioMessage.setRspField(22,bean.getPosData().getEntryMode());
		if(GateWayConstant.FIELD_22.equals(bean.getPosData().getEntryMode())){
			ioMessage.setRspField(26, GateWayConstant.FIELD_26);						//服务点PIN 采集代码
			//转加密pin
			String newPin = new HsmService().pin_tpk2zpk(bean.getPosData().getTpk(), 
					KeyContainer.getInstance().getChanZPK(), bean.getPosData().getPin(), bean.getPosData().getCardNum());
			ioMessage.setRspField(52, ByteUtil.hexStringToBytes(newPin));				//个人密码
			ioMessage.setRspField(53, GateWayConstant.FIELD_53);						//安全控制信息
		}
		
		ioMessage.setRspField(25, GateWayConstant.FIELD_25_CTDREPAYMENT); 					// 25 服务点条件码	n2	M	M	“81”
		ioMessage.setRspField(32, PropertyLoader.getInstance().getChanAcquirerPosID());		//受理机构标识码
		ioMessage.setRspField(33, PropertyLoader.getInstance().getChanForwardPosID());		//发送机构标识码(填接入渠道代码)
		
		String track2 = bean.getPosData().getTrack2();											//磁道2
		if(track2.length() > 37){
			track2 = track2.substring(0,37);
		}
		ioMessage.setRspField(35, track2);													
		
		if(bean.getPosData().getTrack3()!= null && bean.getPosData().getTrack3().length() > 0){		//磁道3
			String track3 = bean.getPosData().getTrack3();
			if(track3.length() > 104){
				track3 = track3.substring(0,104);
			}
			ioMessage.setRspField(36, track3);
		}
		
		ioMessage.setRspField(37, SerialNumber.getChanRetrievalNumber());				//系统检索号
		ioMessage.setRspField(41, PropertyLoader.getInstance().getChanTerminalID());		//受卡机终端标识码
		ioMessage.setRspField(42, PropertyLoader.getInstance().getChanParterID());			//受卡方标识码
		ioMessage.setRspField(43, ByteUtil.fillString(PropertyLoader.getInstance().getChanParterName(), ' ', 40, true));	//受卡方名称地址
		
		StringBuffer fild48 = new StringBuffer();
		fild48.append(GateWayConstant.FIELD_48_CREDIT_REPAY_TAG);
		fild48.append(GateWayConstant.FIELD_48_CREDIT_REPAY_USERTYPE);
		String payNo = ByteUtil.fillString(bean.getCreditAccount(), ' ', 20, true);
		fild48.append(payNo);
		fild48.append(GateWayConstant.FIELD_48_CREDIT_REPAY_MONTH);
		fild48.append(GateWayConstant.FIELD_48_END);
		ioMessage.setRspField(48, fild48.toString());							
		
		ioMessage.setRspField(49,GateWayConstant.FIELD_49); //交易货币代码
		ioMessage.setRspField(60, GateWayConstant.FIELD_60);						//自定义域
		new MacGenerator().generateChanMac(ioMessage); //生成mac
		ioMessage.createResponseMsg();
		return ioMessage;
	}
	
	/**
	 * 组装单笔代收报文
	 * @return
	 * @throws Exception
	 */
	public static IOMessage packSingleCollecting(SigleColt bean)throws Exception{
		IOMessage ioMessage = new IOMessage(GateWayConstant.MSG_CODE_TEMPLATE_ID);
		ioMessage.setRspMesType(GateWayConstant.MSG_CODE_COLLECTION_REQ); //报文类型	n4
		ioMessage.setRspField(3, GateWayConstant.FIELD_3_SIGLECOLT); // 3 处理码	n6 	190000
		ioMessage.setRspField(4, ByteUtil.fillString(bean.getAmount(), '0', 12, false));		//交易金额
		ioMessage.setRspField(7, DateUtil.formatDate("MMddHHmmss")); // 7 传输日期和时间	n10(MMDDhhmmss)
		ioMessage.setRspField(11, SerialNumber.getMerchanSequence()); 			// 11 系统跟踪号
		ioMessage.setRspField(12, DateUtil.formatDate("HHmmss")); // 12 本地交易时间	n6(hhmmss)
		ioMessage.setRspField(13, DateUtil.formatDate("MMdd")); //13 本地交易日期	MMDD
		ioMessage.setRspField(18, PropertyLoader.getInstance().getActiveMerParterType()); //商户类型
		ioMessage.setRspField(22, "012"); //服务点输入方式码
		
		ioMessage.setRspField(25, GateWayConstant.FIELD_25_COLLECTION); // 25 服务点条件码	n2	M	M	“81”
		ioMessage.setRspField(32, PropertyLoader.getInstance().getActiveMerAcquirerPosID()); // 32 	受理机构标识码	n..11(LLVAR) 
		ioMessage.setRspField(33, PropertyLoader.getInstance().getActiveMerForwardPosID());  //33	发送机构标识码	n..11(LLVAR) 	M	M 	填接入渠道代码
		
		ioMessage.setRspField(41, PropertyLoader.getInstance().getActiveMerTerminalID()); //受卡机终端标识码
		ioMessage.setRspField(42, PropertyLoader.getInstance().getActiveMerParterID()); //受卡方标识码
		
		StringBuffer fild48 = new StringBuffer();
		fild48.append(GateWayConstant.FIELD_48_COLLECTION_TAG);
		fild48.append(GateWayConstant.FIELD_48_COLLECTION_USERTYPE);
		String payNo = ByteUtil.fillString("ZD25"+bean.getPayType()+bean.getPayNo(), ' ', 40, true);
		fild48.append(payNo);
		fild48.append(GateWayConstant.FIELD_48_RELATIONSHIP_AREACODE1);
		fild48.append(GateWayConstant.FIELD_48_RELATIONSHIP_AREACODE2);
		fild48.append(GateWayConstant.FIELD_48_COLLECTION_USERTYPE);
		fild48.append(payNo);
		fild48.append(GateWayConstant.FIELD_48_COLLECTION_INDEX);
		fild48.append(ByteUtil.fillString(PropertyLoader.getInstance().getActiveMerParterID(), ' ', 15, true));
		fild48.append(ByteUtil.fillString(PropertyLoader.getInstance().getActiveMerParterName(), ' ', 40, true));
		fild48.append("0202");				//渠道代理商业务代码	TODO
		fild48.append(GateWayConstant.FIELD_48_END);
		ioMessage.setRspField(48, fild48.toString());											
		ioMessage.setRspField(49,GateWayConstant.FIELD_49); 	//交易货币代码
		
		
		String parterID = PropertyLoader.getInstance().getActiveMerParterID();
		String field103;
		if(parterID.length() > 11){
			field103 = parterID.substring(parterID.length()-11,parterID.length());
		}else{
			field103 = ByteUtil.fillString(parterID,' ',11,true);
		}
		ioMessage.setRspField(103,field103); 	//帐户标识2
		new MacGenerator().generateMarchantMac(ioMessage,KeyContainer.getInstance().getMerZAK()); //生成mac
		ioMessage.createResponseMsg();
		return ioMessage;
	}
}
